import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/home/Home.vue'
import Login from '../views/login/Login.vue'
import Compose from '../views/home/Compose.vue'
import Frommoney from '../views/home/Frommoney.vue'
import Tomoney from '../views/home/Tomoney.vue'
import User from '../views/home/User.vue'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: Home,
    name: 'home',
    redirect: '/compose',
    meta: {
      isAute: true
    },
    children: [
      {
        path: '/compose',
        component: Compose,
        name: 'compose'
      },
      {
        path: '/frommoney',
        component: Frommoney,
        name: 'frommoney'
      },
      {
        path: '/tomoney',
        component: Tomoney,
        name: 'tomoney'
      },
      {
        path: '/user',
        component: User,
        name: 'user'
      }
    ]
  },
  {
    path: '/login',
    component: Login,
    name: 'login'
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to, from, next) => {
  // console.log(to.matched)
  if (to.matched.some(item => item.meta.isAute)) {
    const token = localStorage.getItem('token')
    if (token) {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }
})
export default router
